
package de.dtools.n2g.core;

/**
 *	@project:	note2go
 *	@author:	D.Toepel
 *	@version:	2009-08-04
 *	@note:		---
 *	
 *	Copyright (C) 2009  D.Toepel <de.dtools> {at} <gmail> {dot} <com>
 *	
 *	This program is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *	
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *	
 *	You should have received a copy of the GNU General Public License
 *	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *	
 */


import java.awt.Component;
import java.awt.Dimension;

import javax.swing.Icon;
import javax.swing.JTree;
import javax.swing.ToolTipManager;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;

import de.dtools.n2g.res.Resource;

public class LabelTree extends JTree{

	private static final long serialVersionUID = 1L;
	private Resource res = new Resource();

//	protected static LabelModel model;
	
	
	
	public void setSelectedChild(TreePath path){
		this.setSelectionPath(path);
	}

    public LabelTree(Label root) {
        super(new LabelModel(root));
        this.getSelectionModel().setSelectionMode(
        		TreeSelectionModel.SINGLE_TREE_SELECTION);
        this.setRootVisible(false);
        this.expandRow(0);
        this.setExpandsSelectedPaths(true) ;
		ToolTipManager.sharedInstance().registerComponent(this);

		this.setCellRenderer(new LabelRenderer());    
	}
    
    public void select(Label node){
    	Object[] nodes = ((LabelModel) this.getModel()).getTreePathToRoot((TreeNode)node);
		TreePath path = new TreePath(nodes);
		this.setSelectionPath(path);
    }
    
	private class LabelRenderer extends DefaultTreeCellRenderer {
		private static final long serialVersionUID = 1L;

        public Component getTreeCellRendererComponent(
                            JTree tree,
                            Object value,
                            boolean sel,
                            boolean expanded,
                            boolean leaf,
                            int row,
                            boolean hasFocus) {

            super.getTreeCellRendererComponent(
                            tree, value, sel,
                            expanded, leaf, row,
                            hasFocus);
            
            switch (((Label)value).getLabelID()){
    		case -110 : setIcon(res.getLabelCommon);
    			break;
    		case -120 : setIcon(res.getLabelOpen);
				break;
//    		case -130 : setIcon(res.getLabelTags);
//    			break;
   			case -111 : setIcon(res.getLabelHome);
    			break;
    		case -112 : setIcon(res.getLabelResult);
    			break;
    		case -113 : setIcon(res.getLabelOptions);
    			break;
    		case -130 : 
    		case -131 :
    		case -132 : setIcon(res.getTagFolderIcon());
				break;
			default: 
				if(leaf){
					if(((Label)value).isNote()){
						if(((Label)value).isNewNote()){
							setIcon(res.getLabelNewNote);
						}else{
							setIcon(res.getLabelNote);
						}
					}
					else{
						setIcon(res.getTagLeafIcon());
					}
				}else{
					setIcon(res.getTagFolderIcon());
				}
    		}
            
            
            setToolTipText(((Label) value).getToolTip());
            setMaximumSize(new Dimension(80,20));
            return this;
        }

    }
}
